﻿// Decompiled with JetBrains decompiler
// Type: TaleWorlds.CampaignSystem.LogEntries.CharacterKilledLogEntry
// Assembly: TaleWorlds.CampaignSystem, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E85F8C15-4DF6-4E9C-A58A-29177E40D07A
// Assembly location: D:\steam\steamapps\common\Mount & Blade II Bannerlord\bin\Win64_Shipping_Client\TaleWorlds.CampaignSystem.dll

using Helpers;
using System.Collections.Generic;
using TaleWorlds.CampaignSystem.Actions;
using TaleWorlds.CampaignSystem.Conversation;
using TaleWorlds.Core;
using TaleWorlds.Localization;
using TaleWorlds.ObjectSystem;
using TaleWorlds.SaveSystem;

#nullable disable
namespace TaleWorlds.CampaignSystem.LogEntries
{
  public class CharacterKilledLogEntry : LogEntry, IEncyclopediaLog, IChatNotification, IWarLog
  {
    [SaveableField(120)]
    public readonly Hero Victim;
    [SaveableField(121)]
    public readonly Hero Killer;
    [SaveableField(122)]
    private readonly KillCharacterAction.KillCharacterActionDetail _actionDetail;
    [SaveableField(124)]
    private readonly IFaction VictimMapFaction;
    [SaveableField(125)]
    private readonly IFaction KillerMapFaction;

    internal static void AutoGeneratedStaticCollectObjectsCharacterKilledLogEntry(
      object o,
      List<object> collectedObjects)
    {
      ((LogEntry) o).AutoGeneratedInstanceCollectObjects(collectedObjects);
    }

    protected override void AutoGeneratedInstanceCollectObjects(List<object> collectedObjects)
    {
      base.AutoGeneratedInstanceCollectObjects(collectedObjects);
      collectedObjects.Add((object) this.Victim);
      collectedObjects.Add((object) this.Killer);
      collectedObjects.Add((object) this.VictimMapFaction);
      collectedObjects.Add((object) this.KillerMapFaction);
    }

    internal static object AutoGeneratedGetMemberValueVictim(object o)
    {
      return (object) ((CharacterKilledLogEntry) o).Victim;
    }

    internal static object AutoGeneratedGetMemberValueKiller(object o)
    {
      return (object) ((CharacterKilledLogEntry) o).Killer;
    }

    internal static object AutoGeneratedGetMemberValue_actionDetail(object o)
    {
      return (object) ((CharacterKilledLogEntry) o)._actionDetail;
    }

    internal static object AutoGeneratedGetMemberValueVictimMapFaction(object o)
    {
      return (object) ((CharacterKilledLogEntry) o).VictimMapFaction;
    }

    internal static object AutoGeneratedGetMemberValueKillerMapFaction(object o)
    {
      return (object) ((CharacterKilledLogEntry) o).KillerMapFaction;
    }

    public bool IsVisibleNotification => true;

    public override ChatNotificationType NotificationType
    {
      get => this.CivilianNotification((IFaction) this.Victim.Clan);
    }

    public override CampaignTime KeepInHistoryTime => CampaignTime.Never;

    public CharacterKilledLogEntry(
      Hero victim,
      Hero killer,
      KillCharacterAction.KillCharacterActionDetail detail)
    {
      this.Victim = victim;
      this.Killer = killer;
      this.VictimMapFaction = victim.MapFaction;
      this.KillerMapFaction = killer?.MapFaction;
      this._actionDetail = detail;
    }

    public override int AsReasonForEnmity(Hero potentialKiller, Hero potentialRelative)
    {
      return this.Killer != null && potentialKiller == this.Killer && !potentialRelative.Clan.IsMapFaction && potentialRelative.Clan == this.Victim.Clan ? 10 : 0;
    }

    public override string ToString() => this.GetEncyclopediaText().ToString();

    public override TextObject GetHistoricComment(Hero talkTroop)
    {
      if (this.Killer == null)
        return TextObject.Empty;
      ConversationHelper.HeroRefersToHero(talkTroop, this.Victim, true);
      MBTextManager.SetTextVariable("HERO_1", this.Killer.Name, false);
      MBTextManager.SetTextVariable("HERO_2", this.Victim.Name, false);
      return GameTexts.FindText("str_responsible_of_death_news");
    }

    public TextObject GetNotificationText()
    {
      TextObject parent = TextObject.Empty;
      if (this._actionDetail == KillCharacterAction.KillCharacterActionDetail.DiedOfOldAge)
      {
        parent = new TextObject("{=5GSrvawr}{VICTIM.NAME} died of old age. {?VICTIM.GENDER}Her{?}His{\\?} family and friends will remember {?VICTIM.GENDER}her{?}him{\\?}.");
        StringHelpers.SetCharacterProperties("VICTIM", this.Victim.CharacterObject, parent);
      }
      else if (this._actionDetail == KillCharacterAction.KillCharacterActionDetail.Murdered)
      {
        if (this.Killer != null)
        {
          parent = GameTexts.FindText("str_responsible_of_death_link_news");
          StringHelpers.SetCharacterProperties("HERO_1", this.Killer.CharacterObject, parent);
          StringHelpers.SetCharacterProperties("HERO_2", this.Victim.CharacterObject, parent);
        }
        else
        {
          parent = GameTexts.FindText("str_murdered_passive_news");
          StringHelpers.SetCharacterProperties("HERO_2", this.Victim.CharacterObject, parent);
        }
      }
      else if (this._actionDetail == KillCharacterAction.KillCharacterActionDetail.DiedInBattle)
      {
        parent = new TextObject("{=BhDWm78v}{VICTIM.NAME} has died in battle.");
        StringHelpers.SetCharacterProperties("VICTIM", this.Victim.CharacterObject, parent);
      }
      else if (this._actionDetail == KillCharacterAction.KillCharacterActionDetail.DiedInLabor)
      {
        parent = GameTexts.FindText("str_notification_maternal_death");
        StringHelpers.SetCharacterProperties("MOTHER", this.Victim.CharacterObject, parent);
      }
      else if (this._actionDetail == KillCharacterAction.KillCharacterActionDetail.Executed)
      {
        if (this.Killer != null)
        {
          parent = new TextObject("{=hB8CU9LP}{VICTIM.NAME} has been executed by {KILLER.NAME}.");
          StringHelpers.SetCharacterProperties("VICTIM", this.Victim.CharacterObject, parent);
          StringHelpers.SetCharacterProperties("KILLER", this.Killer.CharacterObject, parent);
        }
        else
        {
          parent = new TextObject("{=mwbYdaJr}{VICTIM.NAME} has been executed.");
          StringHelpers.SetCharacterProperties("VICTIM", this.Victim.CharacterObject, parent);
        }
      }
      else if (this._actionDetail == KillCharacterAction.KillCharacterActionDetail.Lost)
      {
        parent = new TextObject("{=pVkchhqX}{VICTIM.NAME} was lost.");
        StringHelpers.SetCharacterProperties("VICTIM", this.Victim.CharacterObject, parent);
      }
      return parent;
    }

    public bool IsVisibleInEncyclopediaPageOf<T>(T obj) where T : MBObjectBase
    {
      return (object) obj == this.Victim || (object) obj == this.Killer;
    }

    public TextObject GetEncyclopediaText()
    {
      TextObject parent = TextObject.Empty;
      if (this._actionDetail == KillCharacterAction.KillCharacterActionDetail.DiedOfOldAge)
      {
        parent = new TextObject("{=KWBwCq1Y}{VICTIM.LINK} died of old age.");
        StringHelpers.SetCharacterProperties("VICTIM", this.Victim.CharacterObject, parent);
      }
      else if (this._actionDetail == KillCharacterAction.KillCharacterActionDetail.Murdered)
      {
        if (this.Killer != null)
        {
          parent = GameTexts.FindText("str_responsible_of_death_link_news");
          StringHelpers.SetCharacterProperties("HERO_1", this.Killer.CharacterObject, parent);
          StringHelpers.SetCharacterProperties("HERO_2", this.Victim.CharacterObject, parent);
        }
        else
        {
          parent = GameTexts.FindText("str_murdered_passive_news");
          StringHelpers.SetCharacterProperties("HERO_2", this.Victim.CharacterObject, parent);
        }
      }
      else if (this._actionDetail == KillCharacterAction.KillCharacterActionDetail.DiedInBattle)
      {
        if (this.KillerMapFaction != null)
        {
          parent = new TextObject("{=kknvzzcG}{VICTIM.LINK} died in a battle against {FACTION_LINK}.");
          parent.SetTextVariable("FACTION_LINK", this.KillerMapFaction.EncyclopediaLinkWithName);
        }
        else
          parent = new TextObject("{=mjSauU7P}{VICTIM.LINK} died in battle.");
        StringHelpers.SetCharacterProperties("VICTIM", this.Victim.CharacterObject, parent);
      }
      else if (this._actionDetail == KillCharacterAction.KillCharacterActionDetail.DiedInLabor)
      {
        parent = GameTexts.FindText("str_notification_maternal_death");
        StringHelpers.SetCharacterProperties("MOTHER", this.Victim.CharacterObject, parent);
      }
      else if (this._actionDetail == KillCharacterAction.KillCharacterActionDetail.Executed)
      {
        if (this.Killer != null)
        {
          parent = new TextObject("{=b6Spbd9O}{VICTIM.LINK} has been executed by {KILLER.LINK}.");
          StringHelpers.SetCharacterProperties("VICTIM", this.Victim.CharacterObject, parent);
          StringHelpers.SetCharacterProperties("KILLER", this.Killer.CharacterObject, parent);
        }
        else
        {
          parent = new TextObject("{=NacogXav}{VICTIM.LINK} has been executed.");
          StringHelpers.SetCharacterProperties("VICTIM", this.Victim.CharacterObject, parent);
        }
      }
      else if (this._actionDetail == KillCharacterAction.KillCharacterActionDetail.Lost)
      {
        parent = new TextObject("{=NKTbhIoi}{VICTIM.LINK} was lost.");
        StringHelpers.SetCharacterProperties("VICTIM", this.Victim.CharacterObject, parent);
      }
      return parent;
    }

    public bool IsRelatedToWar(StanceLink stance, out IFaction effector, out IFaction effected)
    {
      IFaction faction1 = stance.Faction1;
      IFaction faction2 = stance.Faction2;
      effector = this.VictimMapFaction;
      effected = this.KillerMapFaction;
      if (this._actionDetail != KillCharacterAction.KillCharacterActionDetail.DiedInBattle && this._actionDetail != KillCharacterAction.KillCharacterActionDetail.Executed || effector == null || effected == null)
        return false;
      if (faction1 == this.VictimMapFaction && faction2 == this.KillerMapFaction)
        return true;
      return faction2 == this.VictimMapFaction && faction1 == this.KillerMapFaction;
    }
  }
}
